package com.hsnn.medstgmini.pro.base.dao.Impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.base.dao.AnaHospDao;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseDate;

/**
 * @category  医院端统计分析数据返回层实现类
 * @author lil
 * @date 2015年12月07日
 */
@Service
public class AnaHospDaoImpl implements AnaHospDao {
	@Autowired
	private HibernateSqlUtil dao;
	
	/**
	 * @category 获取采购总量日汇总列表信息
	 * @date 2015-12-7
	 * @author lil
	 * @return List<Map<String, String>>
	 */
	@Override
	public Pagination getAnaListByTime(Pagination page) {
		Map<String, String> map =page.getConditions(); 
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.base_amount,t.nonbase_amount,t.total_amount,t.base_ratio,")
		.append("t.del_amount,t.ret_amount,t.del_ratio,t.ret_ratio from ana_hospital t where t.hosp_code = :hospCode ");
		if(StringUtils.isNotBlank(map.get("starttime"))){
			sql.append(" AND t.date >= :starttime ");
		}
		if(StringUtils.isNotBlank(map.get("endtime"))){
			sql.append(" AND t.date <= :endtime ");
		}
		sql.append(" ORDER BY t.date desc ");
		return dao.getSqlPageMapList(sql.toString(), map, page);
	}
	
	/**
	 * @category 获取采购总量月汇总列表信息
	 * @date 2015-12-7
	 * @author lil
	 * @param page
	 * @return
	 */
	@Override
	public Pagination getAnaListByTimeMonth(Pagination page) {
		Map<String, String> map =page.getConditions(); 
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.base_amount,t.nonbase_amount,t.total_amount,t.base_ratio,")
		.append("t.del_amount,t.ret_amount,t.del_ratio,t.ret_ratio from ana_hospital_mon t where t.hosp_code = :hospCode ");
		if(StringUtils.isNotBlank(map.get("starttime"))){
			sql.append(" AND t.date >= :starttime ");
		}
		if(StringUtils.isNotBlank(map.get("endtime"))){
			sql.append(" AND t.date <= :endtime ");
		}
		sql.append(" ORDER BY t.date desc ");
		return dao.getSqlPageMapList(sql.toString(), map, page);
	}
	
	/**
	 * @category 获取药品采购总量季度汇总列表信息
	 * @date 2015-12-7
	 * @author lil
	 * @param page
	 * @return
	 */
	@Override
	public Pagination getAnaListByTimeQuarter(Pagination page) {
		Map<String, String> map =page.getConditions(); 
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.base_amount,t.nonbase_amount,t.total_amount,t.base_ratio,")
		.append("t.del_amount,t.ret_amount,t.del_ratio,t.ret_ratio from ana_hospital_sea t where t.hosp_code = :hospCode ");
		if(StringUtils.isNotBlank(map.get("starttime"))){
			map.put("starttime", map.get("starttime").replace("年", "0").replace("季度", ""));
			sql.append(" AND t.date >= :starttime ");
		}
		if(StringUtils.isNotBlank(map.get("endtime"))){
			map.put("endtime", map.get("endtime").replace("年", "0").replace("季度", ""));
			sql.append(" AND t.date <= :endtime ");
		}
		sql.append(" ORDER BY t.date desc ");
		return dao.getSqlPageMapList(sql.toString(), map, page);
	}
	
	/**
	 * @category 获取药品采购总量年汇总列表信息
	 * @date 2015-12-7
	 * @author lil
	 * @param page
	 * @return
	 */
	@Override
	public Pagination getAnaListByTimeYear(Pagination page) {
		Map<String, String> map =page.getConditions(); 
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.base_amount,t.nonbase_amount,t.total_amount,t.base_ratio,")
		.append("t.del_amount,t.ret_amount,t.del_ratio,t.ret_ratio from ana_hospital_year t where t.hosp_code = :hospCode ");
		if(StringUtils.isNotBlank(map.get("starttime"))){
			sql.append(" AND t.date >= :starttime ");
		}
		if(StringUtils.isNotBlank(map.get("endtime"))){
			sql.append(" AND t.date <= :endtime ");
		}
		sql.append(" ORDER BY t.date desc ");
		return dao.getSqlPageMapList(sql.toString(), map, page);
	}
	
	/**
	 * @category 获取前七天使用量信息
	 * @date 2015-12-7
	 * @author lil
	 * @param hospCode
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getAnaChartByTime(String hospCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.total_amount,t.base_amount,t.nonbase_amount,t.ret_amount,t.del_amount")
		.append(" from ana_hospital t where t.hosp_code = :hospCode and :date <= t.date")
		.append(" ORDER BY t.date ");
		map.put("hospCode", hospCode);
		map.put("date", ParseDate.parseDateToYmd(ParseDate.addDate(new Date(), -7)));
		return (List<Map<String, Object>>)dao.getSqlMapList(sql.toString(), map);
	}
	
	/**
	 * @category 获取前七月使用量信息
	 * @date 2015-12-7
	 * @author lil
	 * @param hospCode
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getAnaChartByTimeMon(String hospCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.total_amount,t.base_amount,t.nonbase_amount,t.ret_amount,t.del_amount")
		.append(" from ana_hospital_mon t where t.hosp_code = :hospCode and :date <= t.date")
		.append(" ORDER BY t.date ");
		map.put("hospCode", hospCode);
		map.put("date", ParseDate.parseDateToYm(ParseDate.addMonth(new Date(), -7)));
		return (List<Map<String, Object>>)dao.getSqlMapList(sql.toString(), map);
	}
	
	/**
	 * @category 获取前七季度使用量信息
	 * @date 2015-12-7
	 * @author lil
	 * @param hospCode
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getAnaChartByTimeSea(String hospCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.total_amount,t.base_amount,t.nonbase_amount,t.ret_amount,t.del_amount")
		.append(" from ana_hospital_sea t where t.hosp_code = :hospCode and :date <= t.date")
		.append(" ORDER BY t.date ");
		map.put("hospCode", hospCode);
		map.put("date", ParseDate.getSea(ParseDate.addMonth(new Date(), -21)));
		return (List<Map<String, Object>>)dao.getSqlMapList(sql.toString(), map);
	}
	
	/**
	 * @category 获取前七年使用量信息
	 * @date 2015-12-7
	 * @author lil
	 * @param hospCode
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getAnaChartByTimeYear(String hospCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer sql =new StringBuffer();
		sql.append("select t.date,t.total_amount,t.base_amount,t.nonbase_amount,t.ret_amount,t.del_amount")
		.append(" from ana_hospital_year t where t.hosp_code = :hospCode and :date <= t.date")
		.append(" ORDER BY t.date ");
		map.put("hospCode", hospCode);
		map.put("date", ParseDate.parseShortYearFormat(ParseDate.addYear(new Date(), -7)));
		return (List<Map<String, Object>>)dao.getSqlMapList(sql.toString(), map);
	}
}
